package com.house.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.house.common.context.BaseContext;
import com.house.common.context.Dictionaries;
import com.house.model.dto.EvaluateDto;
import com.house.model.dto.ExpediteDto;
import com.house.model.dto.SubscribeLookHouseDto;
import com.house.model.entity.SubscribeLookHouse;
import com.house.model.vo.SubscribeLookHouseVo;
import com.house.service.mapper.SubscribeLookHouseMapper;
import com.house.service.service.ISubscribeLookHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SubscribeLookHouseServiceImpl implements ISubscribeLookHouseService {

    @Autowired
    private SubscribeLookHouseMapper subscribeLookHouseMapper;

    @Override
    public void create(SubscribeLookHouse subscribeLookHouse) {
        Object o = new Object();
        String userId = BaseContext.getCurrentId();
        subscribeLookHouse.setUserId(userId);
        subscribeLookHouse.setCreateDate(new Date());
        subscribeLookHouse.setCreatePerson(userId);
        subscribeLookHouse.setStates(Dictionaries.MESSAGE_IS_READ);
        subscribeLookHouse.setExpediteStates(1);
        subscribeLookHouse.setExpediteContent("新订单来了");
        subscribeLookHouseMapper.insert(subscribeLookHouse);
    }

    @Override
    public void changeExpedite(ExpediteDto dto) {
        SubscribeLookHouse subscribeLookHouse = SubscribeLookHouse.builder()
                .id(dto.getId())
                .expediteContent(dto.getExpediteContent())
                .expediteStates(Dictionaries.DEFAULT_EXPEDITE_STATES)
                .build();
        subscribeLookHouseMapper.updateById(subscribeLookHouse);
    }

    @Override
    public void changeStates(SubscribeLookHouseDto dto) {
        SubscribeLookHouse build = SubscribeLookHouse.builder()
                .id(dto.getId())
                .states(dto.getStates())
                .build();
        subscribeLookHouseMapper.updateById(build);
    }

    @Override
    public List<SubscribeLookHouseVo> getMySubscribeLookHouseList() {
        String userId = BaseContext.getCurrentId();
        List<SubscribeLookHouseVo> subscribeLookHouseVos = subscribeLookHouseMapper.selectMyListByUserId(userId);
        return subscribeLookHouseVos;
    }

    @Override
    public List<SubscribeLookHouseVo> getToSubscribeLookHouseList() {
        String userId = BaseContext.getCurrentId();
        List<SubscribeLookHouseVo> subscribeLookHouseVos = subscribeLookHouseMapper.selectToListByUserId(userId);
        subscribeLookHouseMapper.updateExpediteStates(userId);
        return subscribeLookHouseVos;
    }

    @Override
    public Long getMyListCount() {
        String userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<SubscribeLookHouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SubscribeLookHouse::getUserId, userId);
        Long aLong = subscribeLookHouseMapper.selectCount(queryWrapper);
        return aLong;
    }

    @Override
    public void evaluate(EvaluateDto dto) {
        SubscribeLookHouse build = SubscribeLookHouse.builder()
                .id(dto.getId())
                .states(dto.getStates())
                .starLevel(dto.getStarLevel())
                .build();
        subscribeLookHouseMapper.updateById(build);
    }

    @Override
    public boolean hotspot() {
        String userId = BaseContext.getCurrentId();
        int i = subscribeLookHouseMapper.selectHotpot(userId);
        return i > 0;
    }
}
